from selenium import webdriver
from bs4 import BeautifulSoup

import json
import sys
import os


def getHTML(url):
    driver = webdriver.Chrome()
    driver.get(url)
    return driver.page_source


def parse(html, fname):
    res = BeautifulSoup(html, "html.parser")
    allLi = res.find_all(class_="pro-panels")
    for li in allLi:
        try:
            phoneName = li.find(class_="p-img").find("a").attrs["title"]
            phoneDetialLink = li.find(class_="p-img").find("a").attrs["href"]
            phoneCoverImg = li.find(class_="p-img").find("a").find("img").attrs["src"]
            phonePrice = li.find(class_="p-price").find("b").text

            url2 = "https://www.vmall.com" + phoneDetialLink
            # print(url2)
            html2 = getHTML(url2)
            res2 = BeautifulSoup(html2, "html.parser")
            phoneDetailNmae = res2.find(id="pro-name").text
            detialImages = res2.find(id="pro-gallerys").find_all("li")
            detialImagesList = []
            for img in detialImages:
                images = img.find("img").attrs["src"]
                detialImagesList.append(images)

            contentImages = res2.find(class_="pro-detail-see").find("div").find_all("p")
            contentImagesList = []
            for item in contentImages:
                try:
                    target = item.find("img").attrs["data-src"]
                    contentImagesList.append(target)
                except:
                    continue
            phoneInfos = []
            for item in (
                res2.find(class_="product-parameter-list")
                .find("ul")
                .find_all("li")  # product-parameter-list  product-parameter-main
            ):
                try:
                    provideName = item.find("label").text
                    provideNameVal = item.find("span").text
                    # print(provideName)
                    # print(provideNameVal)
                    obj = {"provideName": provideName, "provideNameVal": provideNameVal}
                    phoneInfos.append(obj)
                except:
                    continue
            print(phoneDetailNmae)
        except:
            continue
        yield {
            "phoneName": phoneName,
            "phoneCoverImg": phoneCoverImg,
            "phonePrice": phonePrice,
            "phoneDetailNmae": phoneDetailNmae,
            "detialImagesList": detialImagesList,
            "contentImagesList": contentImagesList,
            "phoneInfos": phoneInfos,
        }


def main():
    url = "https://www.vmall.com/list-237"  ## 华为荣耀手机
    fn = os.getcwd()
    fname = fn + r"\华为热销配件\生活周边.json"
    html = getHTML(url)
    data = parse(html, fname)
    arr = []
    with open(fname, "a", encoding="utf-8") as f:
        arr = []
        for item in data:
            arr.append(item)
        f.write(json.dumps(arr, ensure_ascii=False))


if __name__ == "__main__":
    main()

